<template>
    <div class="login_header">
        <div class="login_box">
            <!-- 图像区域 -->
            <div class="login_img">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登录区域 -->
            <el-form label-width="0px" :model="loginform" class="form" :rules="loginrules" ref="loginformref">
                <el-form-item prop="username">
                    <el-input prefix-icon="iconfont icon-user" v-model="loginform.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="iconfont icon-3702mima" v-model="loginform.password" type="password">
                    </el-input>
                </el-form-item>
                <el-form-item class="button">
                    <el-button type="primary" @click="loginbtn">登录</el-button>
                    <el-button type="info" @click="loginref">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { loginApi } from '@/api/index'
export default {
    name: 'app',
    data() {
        return {
            loginform: {
                username: 'admin',
                password: '123456'
            },
            loginrules: {
                username: [
                    { required: true, message: '请输入用户账号', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入用户密码', trigger: 'blur' },
                    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        loginref() {
            this.$refs.loginformref.resetFields()
        },
        loginbtn() {
            this.$refs.loginformref.validate(async valid => {
                if (!valid) return
                const { data: res } = await loginApi({
                    method: "POST",
                    url: "login",
                    data: this.loginform
                })
                if (res.meta.status !== 200) return this.$message.error('账号或密码输入错误')
                this.$message.success('登录成功')
                console.log(res)
                window.sessionStorage.setItem('token', res.data.token)
                this.$router.push('/home')
            })
        }
    }
}
</script>

<style lang="less" scoped>
.login_header {
    background-color: #2b4b6b;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login_box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;

    .login_img {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);

        img {
            width: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }

    .form {
        position: absolute;
        bottom: 0;
        box-sizing: border-box;
        padding: 0 20px;
        width: 100%;
    }

    .button {
        display: flex;
        justify-content: flex-end;
    }
}
</style>